const express = require('express')
const Mock = require('mockjs')
const router = express.Router()

// GET
router.get('/', (req, res) => {
  // 分页查询列表数据
  const { page = 1, pageSize = 10, action } = req.query

  // 查找所有数据
  if (action === 'all') {
    res.json({
      code: 200,
      data: Mock.mock({
        'list|400': [
          {
            'id|+1': (page - 1) * pageSize + 1,
            'clazz|1': ['二年级一班', '二年级二班', '三年级一班', '四年级一班'],
            stuNo: '@string("number", 8)',
            name: '@cname',
            'type|1': [1, 2], // 1. 额温  2. 耳温
            'temperature|35-37.1': 36.2,
            address: '@county(true)',
            'isOut|1': [0, 1], // 0. 否 1. 是
            'symptom|1': ['无', '咳嗽', '发热', '胸闷'],
            createTime: '@datetime("yyyy-MM-dd HH:mm:ss")'
          }
        ]
      })
    })
    return
  }

  // mock 数据
  const data = Mock.mock({
    [`list|${pageSize}`]: [
      {
        'id|+1': (page - 1) * pageSize + 1,
        'clazz|1': ['二年级一班', '二年级二班', '三年级一班', '四年级一班'],
        stuNo: '@string("number", 8)',
        name: '@cname',
        'type|1': [1, 2], // 1. 额温  2. 耳温
        'temperature|35-37.1': 36.2,
        address: '@county(true)',
        'isOut|1': [0, 1], // 0. 否 1. 是
        'symptom|1': ['无', '咳嗽', '发热', '胸闷'],
        createTime: '@datetime("yyyy-MM-dd HH:mm:ss")'
      }
    ],
    page: Number(page),
    pageSize: Number(pageSize),
    'total': 400,
  })
  
  res.json({
    code: 200,
    data
  })
})

// POST：添加
router.post('/', (req, res) => {
  // 待添加数据
  const {clazz, stuNo, name, type, temperature, address, isOut, symptom} = req.body

  // 数据库操作
  // TODO......

  // 返回
  res.json({
    code: 200,
    data: {
      id: 401,
      clazz,
      stuNo,
      name,
      type,
      temperature,
      address,
      isOut,
      symptom,
      createTime: new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString().replace(/(.*)T(.*)\.\d{3}Z/i, (v, a, b) => a + ' ' + b)
    }
  })
})

// PUT
router.put('/:id', (req, res) => {
  // 待修改数据
  const {id} = req.params
  const {clazz, stuNo, name, type, temperature, address, isOut, symptom, createTime} = req.body

  // 返回
  res.json({
    code: 200,
    data: {
      status: 200,
      message: '修改成功'
    }
  })
})

// DELETE
router.delete('/:id', (req, res) => {
  const {id} = req.params
  res.json({
    code: 200,
    data: {
      status: 200,
      message: '删除成功'
    }
  })
})

module.exports = router
